<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/30
 * Time: 17:45
 */

namespace app\admin\controller;
use app\common\model\Music;
use app\common\model\RoomBackground;
use app\common\model\RoomCategory;
use app\common\model\Room;

use Qiniu\Auth;
use think\Db;
use wheat\Wheat;

class Play extends Base
{


    public static $room_type = [
//        1=>电台 2=>娱乐  3=>点单 4=>聊天
        '1' =>['type_name'=>'电台','type'=>'1'],
        '2' =>['type_name'=>'娱乐','type'=>'2'],
        '3' =>['type_name'=>'点单','type'=>'3'],
        '4' =>['type_name'=>'聊天','type'=>'4'],
    ];

    /**
     * 直播列表
     */
    public function index()
    {

        $where = [];
        if (!empty($_GET['room_name'])) $where['room_name'] = ['like','%'.trim(input('get.room_name')).'%'];
        if (!isEmpty($_GET['status'])) $where['status'] = input('get.status');
        if (!isEmpty($_GET['type'])) $where['type'] = input('get.type');
        $model  = new Room();
        $rows = $model->getList($where);
        $data = Db::name('room_category')->order('create_time DESC')->select();
        $cate = array_key($data,'cid');
        $this->assign([
            'rows' => $rows,
            'title' => '直播列表',
            'cate' => $cate,
            'pageHTML' => $rows->render(),
            'type' => self::$room_type,
        ]);
        return view();

    }

    /**
     * 改变推荐状态
     */
    public function cate_top()
    {

        $this->_change('room_category','top');

    }

    /**
     * 改变推荐状态
     */
    public function top()
    {
        $data = input();
        $result = Db::name('room')->update(['top'=>$data['val'],'update_time'=>time(),'room_id'=>$data['id']]);
        if($result !== false){
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }

    public function edit()
    {
        if(request()->isPost()){

            $data  = input('post.');


            if (!is_numeric($data['user_id'])){
                $this->error('用户id错误');
            }

            $name = Db::name('room')
                ->where('room_name',$data['room_name'])
                ->where('room_id','<>',$data['id'])
                ->find();
            if ($name){
                $this->error('该昵称已存在');
            }


            if ($data['id']){
                $detail = (new Room())->detail(['room_id'=>$data['id']]);
                //房主id
                $user_id = $detail['user_id'];
                $type = $detail['type'];
            }else{
                $user_id = 0;
                $type = $data['type'];
                $data['bg_id'] = Db::name('room_background')->where('status',1)->order('sort')->value('bg_id');
            }

            $model = new \app\common\logic\Room();
            $result = $model->saveChange($data,true);


            if($result !== false){



                if (is_numeric($data['user_id']) && $user_id != $data['user_id']){
                    //修改了房主给房主推消息
                    $str = sprintf('系统管理员把你设为了房间[%s]的房主',$data['room_name']);
                    if(is_numeric($data['id'])){
                        $room_id = $data['id'];
                    }else{
                        $room_id = $model->room_id;
                    }

                    //聊天和娱乐房更换房主初始化麦序
                    if($type == 2 || $type == 4){
                        Wheat::wheatInit($room_id);
                        \app\api\controller\Base::refreshWheat($room_id);
                    }

                    //发送系统消息
                    \app\api\controller\Base::sendUsrMsg('system',hashid($data['user_id']),$str,['type'=>'createRoom','data'=>$room_id]);
                }
                $this->success('操作成功',url('index'));
            }
            $this->error($model->getError());
        }
        $cate = Db::name('room_category')->order('create_time DESC')->select();
        $data = (new Room())->detail(['room_id'=>input('id')]);
        $this->assign([
            'title' => '房间编辑',
            'cate' => array_key($cate,'cid'),
            'data'=>$data,
            'type' => self::$room_type,
        ]);
        return view();
    }

    public function change()
    {
        $data = input();
        $result = Db::name('room')->update(['status'=>$data['type'],'room_id'=>$data['id']]);
        if($result !== false){
            if ($data['type'] == 0){ //发送系统通知  强退房间内的所有人
                \app\api\controller\Base::sendRoomMsg($data['id'],'exit');
            }
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }

    public function delete()
    {
        $id     = input('id');
        $result = Db::name('room')->delete($id);
        if ($result !== false){
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }


    /**
     * 房间分类标签
     */
    public function cate()
    {
        $where = [];
        if (!empty($_GET['cate_name']))$where['cate_name'] = ['like','%'.trim(input('get.cate_name')).'%'];
        if (!isEmpty($_GET['status']))$where['status'] = input('get.status');
        $model = new RoomCategory();
        $rows = $model->getList($where);
        $this->assign([
            'rows' => $rows,
            'pageHTML' => $rows->render(),
            'title' => '房间分类标签',
        ]);
        return view();
    }

    public function cate_edit()
    {
        $this->_edit('room_category','房间分类标签编辑',url('cate'),false);
        return view();
    }

    public function cate_change()
    {

        $this->_change('room_category');

    }

    public function cate_del()
    {
        $id     = input('id');
        $result = Db::name('play_category')->delete($id);
        if ($result !== false){
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }

    public function bg()
    {

        $model  = new RoomBackground();
        $rows = $model->getList();
        $this->assign([
            'rows' => $rows,
            'title' => '房间背景列表',
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }

    public function bg_edit()
    {
        $this->_edit('room_background','房间背景编辑',url('bg'),'room_background');
        return view();
    }


    public function bg_del()
    {
        $this->_del('room_background');
    }

    /**
     * 音乐列表
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function music()
    {
        $model = new Music();
        $map = [];
        if (is_numeric(input('status'))){
            $map['status'] = input('status');
        }
        if (!empty(input('keyword'))){
            $map['singer|song_name'] = ['like','%'.trim(input('keyword')).'%'];
        }
        $rows = $model->_list($map);
        $this->assign([
            'rows' => $rows,
            'title' => '直播列表',
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }

    public function music_edit()
    {

        if (is_numeric(session('user_id'))){
            $url = url('music');
        }else{
            $url = url();
        }

        $this->_edit('music','音乐编辑',$url,false);
        $auth  = new Auth(config('qiniu')['ACCESSKEY'], config('qiniu')['SECRETKEY']);
        $token = $auth->uploadToken(config('qiniu')['bucket']);
        $domain = config('qiniu')['domain'];
        $this->assign([
            'token'=>$token,
            'domain'=> $domain,

        ]);
        return view();
    }


    public function music_change()
    {

        $this->_change('music');

    }

    public function music_delete()
    {
        $this->_del('music');
    }


}